﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace wp7cinema.View
{
    public partial class TestView : PhoneApplicationPage
    {
        public TestView()
        {
            InitializeComponent();

            play.Click += new RoutedEventHandler(play_Click);
            stop.Click += new RoutedEventHandler(stop_Click);

            Media.AutoPlay = false;

            Media.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(Media_MediaFailed);
            Media.MediaOpened += new RoutedEventHandler(Media_MediaOpened);
            Media.DownloadProgressChanged += new RoutedEventHandler(Media_DownloadProgressChanged);

            browser.Navigating += new EventHandler<NavigatingEventArgs>(browser_Navigating);
            browser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(browser_Navigated);
        }

        void browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            play.Content = "이동끝.....";
        }

        void browser_Navigating(object sender, NavigatingEventArgs e)
        {
            play.Content = "이동중.....";
        }

        void stop_Click(object sender, RoutedEventArgs e)
        {
            Media.Stop();
        }

        void Media_DownloadProgressChanged(object sender, RoutedEventArgs e)
        {
            play.Content = Media.DownloadProgress;
        }

        void Media_MediaOpened(object sender, RoutedEventArgs e)
        {
            Media.Play();
        }

        void Media_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            MessageBox.Show("failed : " + e.ErrorException.Message);
        }

        void play_Click(object sender, RoutedEventArgs e)
        {
            xmlDownload();
            //string url = "http://nhnext.hvod.nefficient.co.kr/naversearch/movie/movieclip/making/M52747_05_700k.wmv?key=MzExOTMxNjgwMzExNDkzMjc3MzIwNTMxMzEwMDRvdmllMDE0TTUyNzQ3XzA1XzcwMGszMjM5MzE1MjAwNmpubmFyYTIzODMxOTEwNTI2NjY=";
            ////url = "http://www.jhepple.com/SampleMovies/niceday.wmv";
            //Media.Source = new Uri(url, UriKind.Absolute);
        }

        private void xmlDownload()
        {
            string url = "http://movie.naver.com/movie/bi/mi/mediaView.nhn?code=52747&mid=13915";
            browser.Navigate(new Uri(url));
            //WebClient client = new WebClient();
            //client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            //client.DownloadStringAsync(new Uri(url));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            string html = e.Result;

            Match m = Regex.Match(html, @"mms://.*=");
            if (m.Success)
            {
                //MessageBox.Show(m.Value);
                string url = m.Value.Replace("mms","http");
                url = "http://download.microsoft.com/download/d/8/7/d8751d87-3c99-4470-8e3e-bf352932601a/ctorrec9billg.wmv";
                //url = "http://www.naver.com";
                Uri uri = new Uri(url, UriKind.Absolute);
                //Media.Source = new Uri(url, UriKind.Absolute);
                browser.NavigateToString(html);
            }

            //MovieSetList = xdoc.Descendants("movieSet")
            //    .Select(movieSet =>
            //        movieSet.Descendants("movie").Select(movie =>
            //                new MovieSimpleInfo
            //                {
            //                    ImgUrl = movie.Attribute("imgurl").Value,
            //                    Description = movie.Attribute("description").Value,
            //                    DetailViewCode = movie.Attribute("detailViewCode").Value,
            //                    Genre = movie.Attribute("genre").Value,
            //                    Point = movie.Attribute("point").Value,
            //                    PreorderCode = movie.Attribute("preorderCode").Value,
            //                    ReleaseDay = movie.Attribute("releaseDay").Value,
            //                    Runtime = movie.Attribute("runtime").Value,
            //                    ShareRate = movie.Attribute("shareRate").Value,
            //                    Title = movie.Attribute("title").Value
            //                }
            //                )
            //        ).ToList<IEnumerable<MovieSimpleInfo>>();
            //MovieSet = movieSetList[0];
        }
    }
}